/**    
 * @{#} RegisterActivity.java Create on 2014-5-26 下午9:51:05    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.skinapp.R;

import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.ConfirmCodeGetTask;
import com.smiier.skin.net.ConfirmCodeGetTask.ConfirmCodeGetRequest;
import com.smiier.skin.net.ConfirmCodeGetTask.ConfirmCodeGetResponse;
import com.smiier.skin.net.UserSetTask;
import com.smiier.skin.net.UserSetTask.UserSetRequest;
import com.smiier.skin.net.UserSetTask.UserSetResponse;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class RegisterSecondStepActivity extends BasicActivity {

	private TextView text_unreceive_sms_code, text_tel, text_seconds;
	private EditText edit_sms_code;
	private String tel;

	private int identity, time = 120;

	private int ConfirmCodeType = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_2);
		init();
		setNavTitle("验证手机号");

		tel = getIntent().getStringExtra(Constant.IDENTITY_KEY);
		text_unreceive_sms_code = (TextView) findViewById(R.id.text_unreceive_sms_code);
		text_seconds = (TextView) findViewById(R.id.text_seconds);
		edit_sms_code = (EditText) findViewById(R.id.edit_sms_code);
		text_tel = (TextView) findViewById(R.id.text_tel);
		text_tel.setText(tel.substring(0, 3) + Constant.SPACE
				+ tel.substring(3, 7) + Constant.SPACE + tel.substring(7));

		identity = getIntent().getIntExtra(Constant.IDENTITY_KEY_1, 0);
		if (identity == Constant.TYPE_UPDATE_PASSWORD
				|| identity == Constant.TYPE_FORGET_PASSWORD) {
			ConfirmCodeType = 1;
		} else if (identity == Constant.TYPE_RESET_PAY_PASSWORD) {
			ConfirmCodeType = 2;
		} else if (identity == Constant.TYPE_UPDATE_MOBILE) {
			ConfirmCodeType = 3;
		}
		getSmsCode();
		text_unreceive_sms_code.setClickable(false);
		text_unreceive_sms_code.setTextColor(getResources().getColor(
				R.color.gray));
		new Thread(runTimeThread).start();

	}

	Runnable runTimeThread = new Runnable() {

		@Override
		public void run() {
			while (time > 0) {
				time--;
				timerHandler.sendEmptyMessage(time);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	};

	@SuppressLint("HandlerLeak")
	Handler timerHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			text_seconds.setText(msg.what + "后重新发送");
			if (msg.what == 0) {
				text_seconds.setVisibility(View.GONE);
				text_unreceive_sms_code.setTextColor(getResources().getColor(
						R.color.border_blue));
				text_unreceive_sms_code.setClickable(true);
			}
		}
	};

	public void onClick(View view) {
		int id = view.getId();
		if (id == R.id.btn_left) {
			finish();
		} else if (id == R.id.text_unreceive_sms_code) {
			getSmsCode();
			time = 60;
			text_seconds.setVisibility(View.VISIBLE);
			text_unreceive_sms_code.setClickable(false);
			text_unreceive_sms_code.setTextColor(getResources().getColor(
					R.color.gray));
			new Thread(runTimeThread).start();
			text_unreceive_sms_code.setText("收不到验证码？");
		} else if (id == R.id.btn_submit) {
			String sms_code = edit_sms_code.getText().toString();
			if (!CommonUtility.isNull(sms_code)) {
				Intent intent = null;
				// identity为空时表示注册，不为空则为设置支付密码
				
				
				if(GlobalSettings.login_type == 2){
					
					
					/**
					 * Intent intent = new Intent(getContext(),
									DoctorProfileEditActivity.class);
							intent.putExtra(Constant.IDENTITY_KEY, true);
							startActivity(intent);
					 */
					
					
					
					UserSetRequest req = new UserSetRequest();
					req.mobile = tel;
					req.ConfirmCode = sms_code;
					req.token = GlobalSettings.sToken;
					UserSetTask task = new UserSetTask();
					task.setRequest(req);
					task.addListener(new NetTaskListener<UserSetTask.UserSetRequest, UserSetTask.UserSetResponse>() {
						@Override
						public void onException(
								INetTask<UserSetRequest, UserSetResponse> task,
								Exception e) {

						}

						@Override
						public void onComplete(
								INetTask<UserSetRequest, UserSetResponse> task,
								UserSetResponse response) {
							
							Log.i("testerror", "response.ResultCode:" + response.ResultCode);
							
							
							if (response.ResultCode == 200) {
								/*toast("保存成功");
								GlobalSettings.sUser = response.Res;
								CommonUtility
										.finishActivityFromName(RegisterFirstStepActivity.class
												.getSimpleName());*/
								
								GlobalSettings.sUser.Mobile = tel;
								
								
								Intent intent = new Intent(getContext(),
										DoctorProfileEditActivity.class);
								intent.putExtra(Constant.IDENTITY_KEY, true);
								startActivity(intent);
								finish();
							} else if (response.ResultCode == -2005) {
								CommonUtility.tip(getContext(),
										"您已经认证或者正在等待审核认证中，不可修改数据。");
							} else {
								CommonUtility.tip(getContext(), "修改失败，请稍后再试。");
							}
						}
					});
					add(task);
				}else{
					
					if (identity == Constant.TYPE_RESET_PAY_PASSWORD) {
						intent = getIntent().setClass(this,
								ResetPayPasswordActivity.class);
						intent.putExtra(Constant.IDENTITY_KEY_SMS, sms_code);
						startActivity(intent);
					} else if (identity == Constant.TYPE_UPDATE_MOBILE) {
						UserSetRequest req = new UserSetRequest();
						req.mobile = tel;
						req.ConfirmCode = sms_code;
						req.token = GlobalSettings.sToken;
						UserSetTask task = new UserSetTask();
						task.setRequest(req);
						task.addListener(new NetTaskListener<UserSetTask.UserSetRequest, UserSetTask.UserSetResponse>() {
							@Override
							public void onException(
									INetTask<UserSetRequest, UserSetResponse> task,
									Exception e) {

							}

							@Override
							public void onComplete(
									INetTask<UserSetRequest, UserSetResponse> task,
									UserSetResponse response) {
								if (response.ResultCode == 200) {
									toast("保存成功");
									GlobalSettings.sUser = response.Res;
									CommonUtility
											.finishActivityFromName(RegisterFirstStepActivity.class
													.getSimpleName());
									finish();
								} else if (response.ResultCode == -2005) {
									CommonUtility.tip(getContext(),
											"您已经认证或者正在等待审核认证中，不可修改数据。");
								} else {
									CommonUtility.tip(getContext(), "修改失败，请稍后再试。");
								}
							}
						});
						add(task);
					} else {
						UserSetRequest req = new UserSetRequest();
						req.token = GlobalSettings.sToken;
						UserSetTask task = new UserSetTask();
						task.setRequest(req);
						task.addListener(new NetTaskListener<UserSetTask.UserSetRequest, UserSetTask.UserSetResponse>() {

							@Override
							public void onException(
									INetTask<UserSetRequest, UserSetResponse> task,
									Exception e) {

							}

							@Override
							public void onComplete(
									INetTask<UserSetRequest, UserSetResponse> task,
									UserSetResponse response) {

							}
						});
						add(task);
						intent = getIntent().setClass(this,
								RegisterThirdStepActivity.class);
						intent.putExtra(Constant.IDENTITY_KEY_SMS, sms_code);
						startActivity(intent);
					}
					
				}
			} else {
				toast("请输入手机验证码");
			}
		}
	}

	void getSmsCode() {
		if (!CommonUtility.isNull(tel)) {
			ConfirmCodeGetRequest req = new ConfirmCodeGetRequest();
			req.mobile = tel;
			req.ConfirmCodeType = ConfirmCodeType;
			ConfirmCodeGetTask task = new ConfirmCodeGetTask();
			task.setRequest(req);
			task.addListener(new NetTaskListener<ConfirmCodeGetTask.ConfirmCodeGetRequest, ConfirmCodeGetTask.ConfirmCodeGetResponse>() {

				@Override
				public void onComplete(
						INetTask<ConfirmCodeGetRequest, ConfirmCodeGetResponse> task,
						ConfirmCodeGetResponse response) {
					if (response.ResultCode == 200
							&& response.Res == GlobalSettings.IS_TRUE) {
						toast("验证码发送成功");

					}
				}

				@Override
				public void onException(
						INetTask<ConfirmCodeGetRequest, ConfirmCodeGetResponse> task,
						Exception e) {

				}

			});
			add(task);
		} else {
			toast("请输入手机号");
		}
	}
	
	
/*	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
